package com.cfpl.admin_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_service.mapper.PermissionMapper;
import com.cfpl.admin_service.mapper.RoleMapper;
import com.cfpl.admin_service.service.PermissionService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.Permission;
import com.cfpl.project_common.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 14720
* @description 针对表【permission(权限表)】的数据库操作Service实现
* @createDate 2023-10-10 11:09:53
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>  implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 添加权限
     */
    @Override
    public void insertPermission(Permission permission) {

        if (StrUtil.isBlank(permission.getPermissionName()) || StrUtil.isBlank(permission.getPermissionUrl())){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        permissionMapper.insert(permission);
    }

    /**
     * 修改权限
     */
    @Override
    public void updatePermissionInfo(Permission permission) {

        if (StrUtil.isBlank(permission.getPermissionName()) || StrUtil.isBlank(permission.getPermissionUrl())){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
//        RFID信息记录	/RFIDInfo/record
        permissionMapper.updateById(permission);
    }

    /**
     * 批量删除权限
     * @param permissionIds 权限编号
     */
    @Override
    public void removePermissionsById(List<Long> permissionIds) {
        List<Role> roleList = roleMapper.queryPermissionIsUsing(permissionIds);
        //结果中只要有一条数据，说明有角色正在使用某个要删除的权限信息
        if (!roleList.isEmpty()){
            throw new BusException(CodeEnum.PERMISSION_IS_USING);
        }
        permissionMapper.deleteBatchIds(permissionIds);

    }
}




